/**
    图表统计
*/
<template>
    <div>
        图表

        <div style="text-align:left;">
            <el-select v-model="value" placeholder="请选择" @change="eventChangePic">
                <el-option label="柱形图" value="bar"></el-option>
                <el-option label="折线图" value="line"></el-option>
                <el-option label="饼图" value="pie"></el-option>
            </el-select>
        </div>
        <el-row>
            <el-col :span="12">
                <div id="main" style="width:600px;height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="main2" style="width:600px;height:400px;"></div>
            </el-col>
        </el-row>
    </div>
</template>

<script>

import * as echarts from 'echarts';


export default {
    data() {
        return {
            value: 'line',
            d: [],
            d1: [],
            d2: []
        }
    },
    mounted() {
        this.onloadSaleNumberData()
    },
    methods: {
        //
        eventChangePic() {
            //this.loadEcharts()
            this.displayPic()
        },

        //
        loadEcharts() {

            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));

            //图表需要的数据
            var options = {
                title: {
                    text: '商品的销量'
                },
                tooltip: {},
                xAxis: {
                    data: this.d1 //['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                },
                yAxis: {},
                series: [
                    {
                        name: '销量',
                        type: this.value,
                        data: this.d2 //[5, 20, 36, 10, 10, 20]
                    }
                ]
            }

            // 绘制图表
            myChart.setOption(options);
        },
        loadPieEcharts() {
            var myChart = echarts.init(document.getElementById('main'));

            let options = {
                title: {
                    text: 'Referer of a Website',
                    subtext: 'Fake Data',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                        name: 'Access From',
                        type: 'pie',
                        radius: '50%',
                        data: this.d,
                        // data: [
                        //     { value: 1048, name: 'Search Engine' },
                        //     { value: 735, name: 'Direct' },
                        //     { value: 580, name: 'Email' },
                        //     { value: 484, name: 'Union Ads' },
                        //     { value: 300, name: 'Video Ads' }
                        // ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            myChart.setOption(options);
        },
        onloadSaleNumberData() {
            let url = 'http://localhost:5164/order/sale'
            this.$http.get(url).then(res => {
                this.d1 = res.data.data1
                this.d2 = res.data.data2
                this.d = res.data.data

                this.displayPic()
            })
        },
        displayPic() {
            if (this.value == 'pie') {
                this.loadPieEcharts()
            }
            else {
                this.loadEcharts()
            }
        }
    }
}
</script>